<template>
<Poptip confirm title="确认是否执行？" :disabled="loading" @on-ok="ok" placement="bottom-end" v-if="!isTip">
    <Button v-bind="$attrs" :loading="loading" :icon="icon" :size="size">
        <span>{{text}}</span>
    </Button>
</Poptip>
<Button v-else v-bind="$attrs" :loading="loading" :icon="icon" :size="size" @click="ok">
    <span>{{text}}</span>
</Button>
</template>

<script>
export default {
    props: {
        isTip: {
            type: Boolean,
            default: true
        },
        api: {
            type: Function
        },
        params: {
            type: Object
        },
        styleType: {
            type: String,
            default: "form",
            validator: function (value) {
                // 这个值必须匹配下列字符串中的一个
                return ['tree', 'table', 'form'].indexOf(value) !== -1
            }
        },
        text: {
            type: String,
        }
    },
    data() {
        return {
            loading: false,
            icon: "",
            size: ""
        }
    },
    methods: {
        async ok() {
            this.loading = true

            let params = Object.assign({}, this.params)

            this.$emit('on-before-load', params)

            let res = await this.api(params)

            if (res.isSucceed) {
                this.$Message.success("执行成功");
                this.$emit("on-success", res)
            } else {
                this.$Message.error("执行失败，" + res.message);
            }

            this.loading = false
        }
    },
    created() {
        switch (this.styleType) {
            case "tree":
                this.icon = "ios-remove"
                this.size = "small"
                break;

            case "table":
                this.icon = ""
                this.size = "small"
                break;

            case "form":
                this.icon = ""
                this.size = "default"
                break;

            default:
                break;
        }
    },
}
</script>

<style lang="less" scoped>

</style>
